#include "StdAfx.h"
#include "Astuff.h"
#include "Aobj.h"
#include "astuffman.h"
#include "Alog.h"


Astuff::Astuff( Aobj * obj)
{
	maobj = obj;
	mastuffman = new AstuffMan(maobj);
}
Astuff::~Astuff()
{
	if( mastuffman ) { delete mastuffman; mastuffman = NULL; }
}

// -------------------------------------------
int Astuff::create(){	return 0;}
int Astuff::createAuto() { return 0; }
void Astuff::destory(){}

int Astuff::init(){	return 0;}
int Astuff::initAfter() { return 0; }
void Astuff::exit(){}
void Astuff::exitAfter() {}

int Astuff::reset(){	return 0;}
int Astuff::resetAfter() { return 0; }
int Astuff::start() { return 0; }
int Astuff::startAfter() { return 0; }
//int Astuff::onStart(){	return 0;}
void Astuff::stop() {}
void Astuff::stopAfter() {}
//void Astuff::onStopped() {}

int Astuff::addCreater( Aobj * obj, int size)
{
	//VERIFY(obj);
	Alog::s_out(_T("(auto)->new=0x%X,size=%d,%s.\r\n"), obj, size, obj->getObjName());
	return mastuffman->mobjcreater->add(obj) >=0;
	//VERIFY( mobjcreater->add(obj) >=0 );
}
int Astuff::addInit(Aobj * obj)
{
	//if( maobj)
	//	return maobj->maobjstuff->mobjinit->add(obj);
	//else
	//	return -1;
	return mastuffman->mobjinit->add(obj);
}

// --------------------------------------------
int Astuff::acreate()
{
	if( mastuffman->mobjentercount->check(AobjEnterId::a_creat) > 1)
		return 0;
	//
	int ret = 0;
	Alog::s_out(_T("(auto)->%s: acreate() in.\r\n"), mastuffman->getObjName());
	ret += this->createAuto();
	ret += this->create();

	// add to init list;
	Aobj * obj;
	for( int i=0; i<mastuffman->mobjcreater->getCount(); i++)
	{
		obj = mastuffman->mobjcreater->get(i);
		ret += mastuffman->mobjinit->add(obj);
	}
	Alog::s_out(_T("(auto)->%s: acreate() out=%d.\r\n"), mastuffman->getObjName(), ret );

	return ret;
}
void Astuff::adestory()
{
	if( mastuffman->mobjentercount->check(AobjEnterId::a_destory) > 1) 
		return;
	// exit;
	Alog::s_out(_T("(auto)->%s: adestory() in.\r\n"), mastuffman->getObjName());
	this->aexit();
	// del objs in create list;
	this->destory();
	mastuffman->mobjcreater->delAll();
	Alog::s_out(_T("(auto)->%s: adestory() out.\r\n"), mastuffman->getObjName());
}
int Astuff::ainit()
{
	if( mastuffman->mobjentercount->check(AobjEnterId::a_init) > 1)
		return 0;

	int ret = 0;
	Alog::s_out(_T("(auto)->%s: ainit() in.\r\n"), mastuffman->getObjName());
	ret += this->init();
	ret += mastuffman->mobjinit->initAll();
	ret += this->initAfter();
	Alog::s_out(_T("(auto)->%s: ainit() out=%d\r\n"), mastuffman->getObjName(), ret);

	return ret;
}
void Astuff::aexit()
{
	if( mastuffman->mobjentercount->check(AobjEnterId::a_exit) > 1)
		return;

	Alog::s_out(_T("(auto)->%s: aexit() in.\r\n"), mastuffman->getObjName());
	this->exit();
	mastuffman->mobjinit->exitAll();
	this->exitAfter();
	Alog::s_out(_T("(auto)->%s: aexit() out.\r\n"), mastuffman->getObjName());
}
int Astuff::areset()
{
	Alog::s_out(_T("(auto)->%s: areset() in.\r\n"), mastuffman->getObjName());

	int ret = 0;
	ret += this->reset();

	Aobj * obj;
	for( int i=0; i<mastuffman->mobjinit->getCount(); i++)
	{
		obj = mastuffman->mobjinit->get(i);
		if( obj)
		{
			ret += obj->areset();
		}
	}
	ret += this->resetAfter();

	Alog::s_out(_T("(auto)->%s: areset() out.\r\n"), mastuffman->getObjName());

	return ret;
}
int Astuff::astart()
{
	mastuffman->checkCounterOnStart();		// 20150620;

	// enter only once;
	if( mastuffman->mobjrun->isRun())
		return 0;

	Alog::s_out(_T("(auto)->%s: astart() in.\r\n"), mastuffman->getObjName());

	int ret = this->start();//aonStart();
	if( ret < 0)
	{
		Alog::s_out(_T("(auto)(auto)!>%s: astart() failed, onStart()=%d.\r\n"), 
			mastuffman->getObjName(), ret);
		return ret;
	}

	Aobj * obj;
	for( int i=0; i<mastuffman->mobjinit->getCount(); i++)
	{
		obj = mastuffman->mobjinit->get(i);
		if( obj)
		{
			obj->astart();
		}
	}

	ret += this->startAfter();

	mastuffman->mobjrun->run();
	Alog::s_out(_T("(auto)->%s: astart() out.\r\n"), mastuffman->getObjName());

	return ret;
}
void Astuff::astop()
{
	// enter only once;
	if( !mastuffman->mobjrun->isRun())
		return ;

	if( mastuffman->checkCountOnStop() > 0)	// 20150620;
		return;

	Alog::s_out(_T("(auto)->%s: astop() in.\r\n"), mastuffman->getObjName());

	mastuffman->mobjrun->stop();
	this->stop();

	Aobj * obj;
	for( int i=0; i<mastuffman->mobjinit->getCount(); i++)
	{
		obj = mastuffman->mobjinit->get(i);
		if( obj)
		{
			obj->astop();
		}
	}

	this->stopAfter();

	Alog::s_out(_T("(auto)->%s: astop() out.\r\n"), mastuffman->getObjName());

}
void Astuff::setObjName(const TCHAR * s)
{
	mastuffman->setObjName(s);
}
const TCHAR * Astuff::getObjName()
{
	return mastuffman->getObjName();
}
bool Astuff::isrun()
{
	return mastuffman->mobjrun->isRun();
}